#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
    vector<int> mostCompetitive(vector<int>& nums, int k) {
        vector<int> ans;
        int n = nums.size();
        for (int i = 0; i < n; i++) {
            while (!ans.empty() && k  < n - i + ans.size() && ans[ans.size() - 1] > nums[i]) {
                ans.pop_back();
            }
            ans.push_back(nums[i]);
        }
        ans.resize(k);
        return ans;
    }
};

int main()
{
    vector<int> v({ 5,6,7,8,9,2,2,1,1,1,1 });

    vector<int> ans = Solution().mostCompetitive(v, 5);
    for (auto e : ans) {
        cout << e << " ";
    }
    cout << endl;
    return 0;
}